	var stage_svg_icon = {
		'':'/_assets/icons/blank.svg',
		'think':'/_assets/icons/add.svg',
		'undefined':'/_assets/icons/add.svg',
		'null':'/_assets/icons/add.svg',
		'doing':'/_assets/icons/play-circle.svg',
		'urgent':'/_assets/icons/bomb.svg',
		'farm-out':'/_assets/icons/paper-plane.svg',
		'suspend':'/_assets/icons/pause-circle.svg',
		'finish':'/_assets/icons/add.svg',
		'saved':'/_assets/icons/add.svg'
	};
	
	var stage_icon = {
		'think':'fa-lightbulb-o',
		'undefined':'',
		'null':'',
		'doing':'fa-play',
		'urgent':'fa-bomb',
		'farm-out':'fa-send',
		'suspend':'fa-pause',
		'finish':'fa-flag',
		'saved':'fa-hdd'
	};
	

	
function ifNullToString(obj){
	if(obj==null){
		return '';
	}else{
		return obj.toString();
	}
}	
 function redraw(id){
	$("div.task[data-taskid="+id+"]"+" div.task").empty();
	expend_child(id);
 }
function refresh_one_node(id){
		$.getJSON("/index.php/task/get/"+id, 
			function(dataGet) { 
//				console.log(dataGet);
				if(dataGet){					
					$.each(dataGet,function(idx,item){ 
//						console.log(item);
						$("div.task[data-taskid="+id+"]"+" .task_header").html(new_task_header(item));
					}); 
				}
			}
		);
	hide_toolbar();
	expend_child(id);
//	drag_task_for_calendar();
} 
 
function refresh_child(id){
	if(!id){
		id=current_opt_task_id;
	}
		$.getJSON("/index.php/task/getnode/"+id, 
			function(dataGet) { 
				if(dataGet){
					$("div.task[data-taskid="+id+"] div.task").remove();
					$.each(dataGet,function(idx,item){ 
					
						//$(".task[data-taskid="+id+"]").append(new_task(item));
						//$(".task[data-taskid="+id+"]").children("a.expend_fold").children("i").css("color",'red');
						append_child_task_by_jquery(".task[data-taskid="+id+"]",item);
						
//						alert(item.id);
					}); 
				}
			}
		);
	hide_toolbar();
	expend_child(id);
//	drag_task_for_calendar();
}

function refresh_dom_child(domid){
	if(!domid){
		domid=current_opt_dom_id;
	}
	var taskid=$('#'+current_opt_dom_id).attr("data-taskid");
//	console.log(domid);
//	console.log(taskid);
		$.getJSON("/index.php/task/getnode/"+taskid, 
			function(dataGet) { 
				if(dataGet){
					$("#"+domid+" div").remove();
					$.each(dataGet,function(idx,item){ 
						//$("#"+domid).append(new_task(item));
						//$("#"+domid).children("a.expend_fold").children("i").css("color",'red');
						append_child_task_by_jquery("#"+domid,item);
//						alert(item.id);
					}); 
				}
			}
		);
	hide_toolbar();
	expend_child(taskid);
//	drag_task_for_calendar();
}

function refresh_child_recursion(idArray,sys){
	var action;
	if(sys==1){
		action='getnode_sys';
	}else{
		action='getnode';
	}
//	console.log(idArray);
	var id=idArray.shift();
//	console.log(id);
	if(id==undefined){
		return;
	}
	
	$("div.task[data-taskid="+id+"] div.task").remove();
		$.getJSON("/index.php/task/"+action+"/"+id, 
			function(dataGet,status) {
//				console.log(dataGet);
//				console.log(status);
				if(dataGet){
					$.each(dataGet,function(idx,item){ 
						//$(".task[data-taskid="+id+"]").append(new_task(item));
						//$(".task[data-taskid="+id+"]").children("a.expend_fold").children("i").css("color",'red');
						append_child_task_by_jquery(".task[data-taskid="+id+"]",item);
					}); 
//					idArray.shift();
					expend_child(id);
				}
				if(idArray.length>0){						
						refresh_child_recursion(idArray,sys);
				}else{
						return;
				}
			}
		);
//	hide_toolbar();

//	drag_task_for_calendar();
}

function tasks_refresh_child(idArray){
	var i = idArray.length;
	var id;
	while (i--) {
		id=idArray[i];
//		console.log(id);
		$("div.task[data-taskid="+id+"]"+" div.task").remove();
		$.getJSON("/index.php/task/getnode/"+id, 
			function(dataGet) { 
				if(dataGet){
					$.each(dataGet,function(idx,item){ 
//						$(".task[data-taskid="+id+"]").append(new_task(item));
						append_child_task_by_jquery(".task[data-taskid="+id+"]",item);
					}); 
					expend_child(id);
				}
				
			}
		);
	}
	return;
}
function expend_all_actived_task(){
		$.getJSON("/index.php/task/search_by_stage/doing", 
			function(dataGet) { 
				if(dataGet){
//					$("#"+domid+" div").remove();
//					$(event.target).parents("div.title_task").remove;
					$.each(dataGet,function(idx,item){ 
						append_to_root(item.id,false);
//						$("#"+domid).append(new_task(item));
//						alert(item.id);
					}); 
				}
//				$("#"+domid+">a>i.fa-folder-o").addClass('fa-folder-open-o');
//				$("#"+domid+">a>i.fa-folder-o").removeClass('fa-folder-o');
			}
		);

}



function append_to_parent(id){
	if(!id){
		id=current_opt_task_id;
	}
//	console.log(id);
		var parentid;
		var element;
		$.getJSON("/index.php/task/getparents/"+id, 
			function(dataGet) { 
//			console.log(dataGet);
				if(dataGet.data){
//					$("div.task[data-taskid="+id+"]"+" div").remove();
					parentid=0;
					$.each(dataGet.data,function(idx,item){ 
						element = $(".task[data-taskid="+parentid+"]"+' '+".task[data-taskid="+item.id+"]");
						if(element.length>0){
							
						}else{
//							$(".task[data-taskid="+parentid+"]").append(new_task(item));
							append_child_task_by_jquery(".task[data-taskid="+parentid+"]",item);
						}
						parentid=item.id;
//						console.log($("#task"+item.id));
					}); 
				}
			$("#alltask_view .task[data-taskid="+id+"]").children("span.task_title").addClass('twink');	
			}
		);
	hide_toolbar();	
//	console.log(id);
//	console.log($(".task[data-taskid="+id+"]"));
//	console.log($(".task[data-taskid="+id+"]").children('span.task_title'));
//	expend_child(id);
}

function refresh_update_task(){


	//如果有该任务
	//如果父任务下有该任务，不操作
	//如果该任务没有挂装在父任务下，移动其位置
	//如果没有该任务，则生成一个任务，并挂装到父节点下
	
	//改变任务内容
	
	//var loading=layer.msg('正从服务器上加载数据，请稍候！', {icon: 16});
		var loading = layer.load(2); //换了种风格

		var items;
		$.getJSON("/index.php/task/getlastedit/", 
			function(dataGet) { 
//			console.log(dataGet);
				if(dataGet.data){
//					console.log(dataGet.data);
					items=dataGet.data;
						$.each(items,function(idx,item){ //生成所有任务，先挂装到不分类任务下
							element = $(".task[data-taskid="+item.id+"]");
							parent_element = $(".task[data-taskid="+item.parent+"]"+' '+".task[data-taskid="+item.id+"]");
							parent = $(".task[data-taskid="+item.parent+"]");
							if(parent_element.length>0){//如果已存在该任务,且父子对象位置正确
	//							items.splice(idx,1);
							}else{
								if(parent.length>0){//如果已存在父任务
									if(element.length>0){//且子对象也存在
										$(".task[data-taskid="+item.parent+"]").append(element);
									}else{
										$(".task[data-taskid="+item.parent+"]").append(new_task(item));
										$(".task[data-taskid="+item.id+"]").css('display','none');
									}
								}else{//如果没有父任务，则存到未挂装中
	//								console.log(item);
	//								console.log(idx);
									noappenditems.push(item);
								}
								
							}
							
							
							
                            $(".task[data-taskid=3]").append(new_task(item));
						});
						$(".task[data-taskid=3]>div.task").hide();
                        $.each(items,function(idx,item){ //所有任务依据自己的parnet挂装
//							console.log(item);
							$("#simple_view .task[data-taskid="+item.parent+"]").append($("#simple_view .task[data-taskid="+item.id+"]"));
							$("#alltask_view .task[data-taskid="+item.parent+"]").append($("#simple_view .task[data-taskid="+item.id+"]"));
						});	
//							
					$('#btn_refresh_all i').removeClass('fa-spin');
					expend_icon_status();
					$(".task[data-taskid=1]>div.task").show();
					$(".task[data-taskid=2]>div.task").show();
					
					//关闭
//					layer.close(index); 
					layer.close(loading); 
				}
			}		
		);
		$('#btn_refresh_all i').addClass('fa-spin');  
	
	
}

function create_root_task(){
	var item = new Object;
	item.id = 1;
	item.title = '所有任务';
	item.sort=100;
	item.getway="R";
	$("div#alltask_view div").remove();
	$("#alltask_view").append(new_task(item));
}

function create_one_task_from_db(id,dom_id){
	var htmlstr;
		$.getJSON("/index.php/task/get/"+id, 
			function(dataGet) { 
//				console.log(dataGet);
				if(dataGet){
					htmlstr=new_task(dataGet);					
//					console.log(htmlstr);
						var timestamp = (new Date()).valueOf(); 
//						console.log('1-'+timestamp);
						$(dom_id).append(htmlstr);
//					return htmlstr;//注意要放在这里，否则你可能等不到返回信息
					}
			}
		);

//	console.log(htmlstr);
	var timestamp = (new Date()).valueOf(); 
//	console.log('2-'+timestamp);
	return ;
}

function create_one_task(item,container_dom){
	$(container_dom).append(new_task(item));
	return new_task(item);
}

 function create_title_task(item,container_dom){
//	 console.log(container_dom);
	var new_dom_id = unique_dom_id++;
	var htmlstr='<div  id="'+new_dom_id+'" ';
	htmlstr+=' class="task title_task" data-stage="'+item.stage+'">'
	+'<a class="icon_button expend_fold" href="###"   title="展开"><i  class="fa fa-folder-o"></i></a>'
	+'<span class="task_title">'+item.title+'</span>'
	+'<a class="stage_icon"></a>'
//	+'<span class="task_stage"><i  class="fa '+stage_icon[item.stage]+'"></i></span>'
	+'<a><i  class="fa fa-refresh" onclick="search_by_stage()"></i></a>'
	+'</div>';
//	console.log(htmlstr);
	$(container_dom).append(htmlstr);
 }

 

 function fold_child(id){
	re_draw(id);
 }
 

 
 
 
 
 
 
 


function search_by_stage(domid){
	// if(!domid){
		// var domid=$(event.target).parents("div.title_task").attr('id');
	// }
	var stage=$("#"+domid).attr('data-stage');
	$.getJSON("/index.php/task/search_by_stage/"+stage, 
			function(dataGet) { 
				if(dataGet){
					$("#"+domid+" div").remove();
					$.each(dataGet,function(idx,item){ 
//						console.log(item);
						$("#"+domid).append(new_task(item));
					}); 
				}
				$("#"+domid+">a>i.fa-folder-o").addClass('fa-folder-open-o');
				$("#"+domid+">a>i.fa-folder-o").removeClass('fa-folder-o');
			}
		);
	

}


function init_task_refresh(){
		var idArray= new Array();
		var i=0;
//	refresh_child_recursion(idArray);	
	$.post("/index.php/task/getparents_by_tag/", 
			{
				search_words:'默认打开'
			  },
			function(dataGet) { 
				if(dataGet){
					$.each(dataGet,function(idx,item){ 
						idArray[i++]=item;		
					}); 
//					console.log(idArray);
					refresh_child_recursion(idArray);
				}
			}
		,"json");
		
	
}



function float_view(view_domid){
	var domid;
	if(!view_domid){
		domid=$(event.target).closest(".view_container").attr("id");
	}else{
		domid=view_domid;
	}
	console.log(domid);
	
	if($("#"+domid).css("position")=="static"){
		$("#"+domid).css("position","fixed").css("left","36rem").css("top","20px").css("z-index","5").css("height","90%");
        $("#"+domid+">.view_content").css("height","90%").css("overflow-y","auto");
	}else{
		$("#"+domid).css("position","static").css("height","auto");
        $("#"+domid+">.view_content").css("height","auto");
	}
	console.log($(event.target));
	console.log($(this));
}



